//
//  CXPayProcess.m

//

#import "CXPayProcess.h"
#import <zhPopupController.h>
#import "CXPayTypeTableView.h"
#import "CXPayTypeCollectionView.h"
#import "CXPayTypeModel.h"
#import "CXPayManager.h"
@interface CXPayProcess()<PayTypeChooseDelegate>

/** 类型 */
@property(assign,nonatomic) OrderType  ordertype;
/** 类型 */
@property(assign,nonatomic) ShowType  showtype;
/* <#description#> */
@property (nonatomic ,copy) NSString *payMoney;


/** 订单id */
@property(copy,nonatomic) NSString  *type_id;
/** 控制器 */
@property(strong,nonatomic) UIViewController  *target;


@end
@implementation CXPayProcess

static CXPayProcess *instance = nil;

+(instancetype)shareInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc]init];
    });
    return instance;
}

-(void)payProcessMethodType:(OrderType)orderType andShowType:(ShowType)showType WithProcess:(NSString *)processid andMoney:(NSString *)payMoney andTarget:(UIViewController *)target completedCallBack:(PayCompletedCallBack)completedCallBack
{
    self.ordertype = orderType;
    self.showtype = showType;
    self.payMoney = payMoney;
    self.type_id = processid;
    self.target = target;
    self.payCompletedCallBack = completedCallBack;
    
    [self loadPaymentDataReback:^(NSMutableArray *dataArray)
     {
         if(showType == kUseShowTypeTableView)
         {
             CXPayTypeTableView *chooseView = [CXPayTypeTableView loadPayTypeView];
             chooseView.width = HNTScreenWidth;
             chooseView.height = 200+49*(dataArray.count)+HNTBottomHeight;
             chooseView.dataArray = dataArray;
             chooseView.delegate = self;
             chooseView.amountLabel.text = [NSString stringWithFormat:@"支付￥%@",payMoney];
             target.zh_popupController = [zhPopupController new];
             target.zh_popupController.maskAlpha = 0.3;
             target.zh_popupController.layoutType = zhPopupLayoutTypeBottom;
             target.zh_popupController.allowPan = NO;
             target.zh_popupController.dismissOnMaskTouched = NO;
             [target.zh_popupController presentContentView:chooseView duration:0.25 springAnimated:NO];
         }
         else
         {
             CXPayTypeCollectionView *chooseView = [CXPayTypeCollectionView loadPayTypeCollectionView];
             chooseView.width = HNTScreenWidth;
             chooseView.height = 250+HNTBottomHeight;
             chooseView.dataArray = dataArray;
             chooseView.payMount = payMoney;
             chooseView.delegate = self;
             
             target.zh_popupController = [zhPopupController new];
             target.zh_popupController.maskAlpha = 0.3;
             target.zh_popupController.layoutType = zhPopupLayoutTypeBottom;
             target.zh_popupController.allowPan = NO;
             target.zh_popupController.dismissOnMaskTouched = NO;
             [target.zh_popupController presentContentView:chooseView duration:0.25 springAnimated:NO];
         }
     }];
}

-(void)view:(UIView *)view WithIsConfirm:(NSInteger)index andModel:(CXPayTypeModel *)model
{
    if (index == 1)
    {
//        NSString *payid = model.pay_id;
        if ([model.pay_id isEqualToString:@"999"])
        {
            [self loadOnlinePayType:kUsePayTypeAliPay andOrderid:self.type_id andReback:^(NSString *result) {
                if (result) {
                    [[CXPayManager sharedInstance] orderPay:kUsePayTypeAliPay targetController:self params:@{@"url":result} completedCallBack:^(NSDictionary * _Nonnull dict) {
//                        NSLog(@"dict!!!!!!%@",dict);
                        NSMutableDictionary *muteDic = [NSMutableDictionary dictionaryWithDictionary:dict];

                        self.payCompletedCallBack(muteDic);
                    }];
                }
                
                
            }];
            
            
        }
        else if ([model.pay_id isEqualToString:@"998"])
        {
            [self loadOnlinePayType:kUsePayTypeWeChatPay andOrderid:self.type_id  andReback:^(NSString *result) {
                if (result) {
                    [[CXPayManager sharedInstance] orderPay:kUsePayTypeWeChatPay targetController:self params:@{@"url":result} completedCallBack:^(NSDictionary *dict) {
                        NSMutableDictionary *muteDic = [NSMutableDictionary dictionaryWithDictionary:dict];

                        self.payCompletedCallBack(muteDic);
                    }];
                }
            }];
        }
        else
        {

            
            
            
            
        }
        
    }else{
        

        self.payCompletedCallBack(@{@"result":@"000"});
    }
    [self.target.zh_popupController dismissWithDuration:0.25 springAnimated:NO];
}


-(void)loadPaymentDataReback:(void(^)(NSMutableArray *dataArray))reback
{

                    NSMutableArray *dataArray = [NSMutableArray arrayWithCapacity:0];
                CXPayTypeModel *model1 = [[CXPayTypeModel alloc]init];
                    model1.name = @"支付宝支付";
                    model1.logo = @"pay-zhifubao";
                    model1.isSelect = @"1";
                    model1.pay_id = @"999";
                    [dataArray addObject:model1];
                    CXPayTypeModel *model2 = [[CXPayTypeModel alloc]init];
                           model2.name = @"微信支付";
                           model2.logo = @"pay-weixin";
                                model2.isSelect = @"0";
                            model2.pay_id = @"998";
                        
                           [dataArray addObject:model2];
                    reback(dataArray);
 
}

-(void)loadOnlinePayType:(UsePayType)payType andOrderid:(NSString *)orderid andReback:(void(^)(NSString  *result))reback
{
    NSString *apiString  = @"";
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]initWithCapacity:0];
    [dic setObject:orderid forKey:@"orderid"];
    switch (payType)
    {
        case kUsePayTypeAliPay:
        {
            [dic setObject:self.type_id forKey:@"orderid"];
            apiString = @"/index/pay/alipay";
        }
            break;
        case kUsePayTypeWeChatPay:
        {
            [dic setObject:self.type_id forKey:@"orderid"];
            apiString = @"/index/pay/weChat";
        }
            break;
  
        default:
            break;
    }
   [[HNTNetworkTool shareTool] request:POST apiString:apiString parameters:dic value:nil finished:^(id _Nonnull dic, NSError * _Nonnull error)
    {
       if (error!=nil)
       {
           reback(nil);
           [MBManager showBriefAlert:error.localizedDescription inView:nil];
       }else
       {
           NSString *status = [NSString stringWithFormat:@"%@",[dic objectForKey:@"code"]];
           if ([status isEqualToString:@"0"])
           {
               reback(dic[@"data"]);
           }
           else
           {
               
               reback(nil);
               [MBManager showBriefAlert:dic[@"msg"] inView:nil];
           }
       }
   }];

}


-(void)scanClicked
{
 
}

@end
